package com.zhang.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhang.reggie.common.R;
import com.zhang.reggie.domain.Employee;
import com.zhang.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.Objects;

/**
 * 实现明天理想的唯一障碍是今天的疑虑
 *
 * @Author {张旭军}
 * @Date: 2023/02/02/ 21:46
 * @description
 */
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Resource
    private EmployeeService employeeService;

    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request,@RequestBody Employee employee){
        String password = employee.getPassword();
        String username = employee.getUsername();

        password = DigestUtils.md5DigestAsHex(password.getBytes(StandardCharsets.UTF_8));
        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Employee::getUsername,username);
        Employee employeeInfo = employeeService.getOne(lambdaQueryWrapper);
        if(Objects.isNull(employeeInfo)){
            return R.error("登录失败,用户名不存在");
        }

        if(!employeeInfo.getPassword().equals(password)){
            return R.error("登录失败，密码错误");
        }
        if(employeeInfo.getStatus() == 0){
            return R.error("账号已经禁用");
        }
        request.getSession().setAttribute("employee",employeeInfo.getId());
        log.info("打印session:"+request.getSession().getAttribute("employee"));
        return R.success(employee);
    }

    @RequestMapping("/logout")
    public R<String> loginOut(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    @RequestMapping
    public R<String> saveEmployee(HttpServletRequest request,@RequestBody Employee employee){

        String username = employee.getUsername();
        String name = employee.getName();
        String sex = employee.getSex();
        String phone = employee.getPhone();
        String idNumber = employee.getIdNumber();

        if(StringUtils.isAllBlank(username,name,sex,phone,idNumber)){
            return R.error("请输入正确的账号，检查填写是否正确");
        }
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        LambdaQueryWrapper<Employee> employeeQueryWrapper = new LambdaQueryWrapper<>();
        employeeQueryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee one = employeeService.getOne(employeeQueryWrapper);
        if(Objects.nonNull(one)){
            return R.error("账号重复,请先解绑");
        }
        Long empId = (Long) request.getSession().getAttribute("employee");
        employee.setCreateUser(empId);
        employee.setUpdateUser(empId);
        employeeService.save(employee);

        return R.success("账号保存成功");
    }

}
